﻿function ValidNumber(txt, isValid) {
    if (isValid == 1) {
        if (!isNaN(txt.value)) {
            return true;
        }

        txt.value = "";
        alert("Numero invalido");
        txt.focus();
        return false;
    }

    return true;
}

function MaxNumber(txt, maxLen) {
    try {
        if (window.event.keyCode != 8 && window.event.keyCode != 9 && window.event.keyCode != 37 &&
            window.event.keyCode != 39 && window.event.keyCode != 46 && window.event.keyCode != 16 &&
            window.event.keyCode != 17 && window.event.keyCode != 33 && window.event.keyCode != 34 &&
            window.event.keyCode != 35 && window.event.keyCode != 36 && window.event.keyCode != 109) {
            // IE version
            var sel = null;
            if (document.selection != undefined) {
                sel = document.selection.createRange();
            }
            
            if (sel.text != "" && sel != null)
                return true;

            var selectedText;
            if (txt.selectionStart != undefined) {
                var startPos = txt.selectionStart;
                var endPos = txt.selectionEnd;
                selectedText = txt.value.substring(startPos, endPos)
            }

            if (selectedText != "" && selectedText != null)
                return true;

            var currentNum = txt.value.length;
            if (txt.value.indexOf(".") >= 0)
                currentNum = currentNum - 1;
            if (txt.value.indexOf("-") >= 0)
                currentNum = currentNum - 1;
            if (currentNum > maxLen - 1)
                return false;
        }

        return true;
    }
    catch (e) {
        //alert(e.Description);
        return true;
    }
}

function MaxNumberChange(txt, maxLen) {
    try {
        var len = txt.value.length;
        if (txt.value.indexOf(".") >= 0)
            maxLen = maxLen + 1;
        if (txt.value.indexOf("-") >= 0)
            maxLen = maxLen + 1;

        if (len > maxLen - 1) {
            txt.value = txt.value.substring(0, maxLen);
        }

        return true;
    }
    catch (e) {
        //alert(e.Description);
        return true;
    }
}

function ValidObject(txt) {
    try {
        var len = txt.value.trim().length;
        var error = "";
        if (len == 0) {
            txt.className = "validatorCalloutHighlight";
            for (j = 0; j < txt.attributes.length; j++) {
                if (txt.attributes[j].name == "errormessage") {
                    error = txt.attributes[j].value;
                }
            }
            txt.title = error == "" ? "Requerido" : error;
            return false;
        }
        else {
            txt.className = "";
            txt.title = "";
        }

        return true;
    }
    catch (e) {
        //alert(e.Description);
        return true;
    }
}

function ValidExpression(obj, expression) {
    if (expression.trim() == "")
        return true;
    var RegEx = new RegExp(expression);
    var valid = RegEx.test(obj.value);
    var error = "";

    if (!valid) {
        obj.className = "validatorCalloutHighlight";
        for (j = 0; j < obj.attributes.length; j++) {
            if (obj.attributes[j].name == "errormessageexpression") {
                error = obj.attributes[j].value;
            }
        }
        obj.title = error == "" ? "Error" : error;
        return false;
    }
    else {
        obj.className = "";
        obj.title = "";
    }

    return true;
}